package org.text2diagram.ddl.domain;

import java.util.ArrayList;

import org.text2diagram.ddl.converter.ConverterException;

public enum Appearance {
	
	BOX("square"), 
	POLYGON, 
	ELLIPSE("oval"), 
	POINT, 
	EGG, 
	TRIANGLE, 
	DIAMOND, 
	TRAPEZIUM, 
	PARALLELOGRAM, 
	HEXAGON, 
	OCTAGON, 
	DOUBLECIRCLE, 
	TRIPLEOCTAGON, 
	INVTRIANGLE, 
	INVTRAPEZIUM, 
	
	//not directly supported by GV
	ROUNDED_SQUARE("roundedSquare", "rounded"), 
	STAR;
	
	private ArrayList<String> names = new ArrayList<String>();
	
	private Appearance() {
		this.names.add(this.toString().toLowerCase());
	}

	private Appearance(String... synonyms) {
		this();
		for (String name : synonyms) 
			this.names.add(name);
	}
	
	@Override
	public String toString() {
		return String.format("Appearance(%s)", this.name());
	}

	public static Appearance findAppearance(String origName) throws ConverterException {
		String name = canonizeName(origName);
		for (Appearance appearance : Appearance.values()) {
			if (appearance.names.contains(name)) {
				return appearance;
			}
		}		
		//no matching appearance found, raise exception with user friendly message
		throw new ConverterException("No matching appearance for name: \"" + name + "\"");
	}
	
	private static String canonizeName(String name) {
		if (name == null)
			return null;
		
		return name.toLowerCase().replaceAll("_", "").replaceAll(" ", "");
	}
	
	public static ArrayList<String> getAllAppearanceNames() {
		ArrayList<String> names = new ArrayList<String>();
		for (Appearance appearance : Appearance.values()) {
			names.addAll(appearance.names);
		}
		return names;
	}

	public ArrayList<String> getNames() {
		return names;
	}
	
}
